package 面试;

public class 死锁 {




    public static void main(String[] args) {
        String resource1="a";
        String resource2="b";
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                synchronized (resource1){

                    try {
                        System.out.println("获得资源"+resource1);

                        Thread.sleep(1000);
                        synchronized (resource2){
                            System.out.println("获得资源"+resource2);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Runnable runnable2=new Runnable() {
            @Override
            public void run() {
                synchronized (resource2){

                    try {
                        System.out.println("获得资源"+resource2);

                        Thread.sleep(1000);
                        synchronized (resource1){
                            System.out.println("获得资源"+resource1);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread t1=new Thread(runnable);
        Thread t2=new Thread(runnable2);
        t1.start();
        t2.start();

    }
}
